/**
 * 专题用的简单弹窗
 * options
 *		content :
 *		clazz  :  // 弹窗节点类名
 *  	top : 
 *		left :
 *		clickMaskClose : false
 *		bodyClickClose : false
 *	 	afterShow : fn
 *		afterClose : fn
 */
(function (jq, g){

	function Dialog(){
		return this.init.apply(this, arguments);
	}

	Dialog.setting = {
		content : '',
		afterShow : null,
		top : null,
		left : null
	}

	var pagePro = Dialog.prototype;

	pagePro.init = function(options){
		if(!options){
			return;
		}

		this.setting = jq.extend({}, Dialog.setting, options);

		if(jq(document.body).find('.u-dialog-mask').length){
			this.masknode = jq(document.body).find('.u-dialog-mask');
		}else{
			this.masknode = jq('<div class="u-dialog-mask"></div>');
		}
		
		this.dialognode = jq('\
				<div class="u-dialog '+ (this.setting.clazz || '') +'">\
					<a class="close"></a>\
					<div class="content"></div>\
				</div>');

		return this;
	}

	pagePro.show = function(options){
		this.showMask();

		this.contentNode = jq(this.setting.content);

		this.dialognode.find('.content').append(this.contentNode);

		this.dialognode.appendTo(document.body);

		var top = this.setting.top != undefined ? this.setting.top : ((jq(window).height() - this.contentNode.outerHeight())/2 + (jq(document).scrollTop() || 0));
		var left = this.setting.left != undefined ? this.setting.left : ((jq(window).width() - this.contentNode.outerWidth())/2);

		top = top < 0 ? 0 : top;
		left = left < 0 ? 0 : left;

		this.dialognode.css({
			'top' : top + 'px',
			'left' : left + 'px'
		});
			
		this.initEvent();

		this.setting.afterShow && this.setting.afterShow.call(this, this.dialognode);

		return this;
	}

	pagePro.initEvent = function() {
		var self = this;

		this.dialognode.find('.close').on('click', function(event) {
			self.close();
		});

		if (this.setting.clickMaskClose) {
			masknode.click(self.close.bind(self));
		};

		// this.dialognode.click(function (event) {
		//     event.stopPropagation();
		// });
	}

	pagePro.showMask = function() {
		this.masknode && this.masknode.appendTo(document.body);
	}

	pagePro.close = function(options){
		this.masknode && this.masknode.remove();
		this.dialognode.remove();

		jq(document.body).unbind('click', self.close);

		this.setting.afterClose && this.setting.afterClose.call(this);

		return this;
	}

	jq.showlDialog = function(options){
		return (new Dialog(options)).show();
	}

})(jQuery, window);